local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHScreenBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local LanguageHelper=_G.LanguageHelper; 
local BtnHelper = ClientOHOH.load.BtnHelper();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local ConnectClass=_G.CommonOHOH.load.Connect();
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();

function InitInsts(ui)
	ui.insts = {};

	ui.insts.root ="Frame.Frame"
	ui.insts.close ="Frame.Frame.close"
	ui.insts.title ="Frame.Frame.title"
	ui.insts.tip ="Frame.Frame.TextLabel"

	ui.insts.skip ="Frame.Frame.skip"
	ui.insts.skipIcon ="Frame.Frame.skip.Icon"
	ui.insts.skipText ="Frame.Frame.skip.TextLabel"

	ui.insts.cells="Frame.Frame.Content";
    ui.insts.content ={}
	ui.insts.cell="Frame.Frame.Content.list"
end;
function M.MobileSize(self)
	if _G.client.IsMobile then --是移动设备
		if self.insts.root then
			local pos= self.insts.root:GetAttribute("Position")
			local size=self.insts.root:GetAttribute("Size")

			if pos then
				self.insts.root.Position=pos
			end
			if size then
				self.insts.root.Size=size
			end
		end
	end
end
function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self);
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.OnLineRewardGui"));
	self:MobileSize();
	self:InitAllButtons();
	self.insts.tip.Text=LanguageHelper:GetText(10401) 
	self.insts.title.Text=LanguageHelper:GetText(10402) 
	self.ClaimAction=ConnectClass.new()
    self.MinTimeAction=ConnectClass.new()
	self.ClaimFreeAction=ConnectClass.new() --免费领取所有未领取过的奖励
	CallBackManager:AddRbxConnect(self,self.insts.close.Activated, function()
        self:Hide()
	end);
end;
function M.InitAllButtons(self)
	self:AddProcess(function()
		for _, v in pairs(self.main_inst:GetDescendants()) do
			if v:IsA("GuiButton") then
				BtnHelper:InitButton(v, nil, nil, nil, function() end);
			end;
		end;
	end);
end
function M.SetData(self,data)
    self.OnlineData=data
    for k,v in pairs(data) do
        local cell= self.insts.content[k];
        self:CreateCellObj(cell,v)
        cell.time.Text=GeneralTool.secondsToHHMM(v.time)
        CallBackManager:AddRbxConnect(self, cell.btn.Activated, function()
                if self.OnlineData[k]:IsCanGet() then
                   local arg= self.ClaimAction:Action(k)
                   for _,v in pairs(arg) do
                        if v then
                            self.OnlineData[k]:Claime()
                            self.insts.content[k].time.Text=LanguageHelper:GetText(10404)
							self.insts.content[k].btn.ImageColor3=self.insts.content[k].colorOld.Value;
                        end
                   end
                end
        end);
    end
	
    self:StartUpdateTime()
end

function M.CreateCellObj(self,ui,cfg)
    local Obj={
        UI=ui,
        data=cfg,
        FullUi=function(this)
            local cell= this.UI
            local v=this.data
            local old=cell.Viewport:FindFirstChildOfClass("Model")
            if old then
                old:Destroy()
            end
			local old2= cell.Viewport:FindFirstChildOfClass("MeshPart");
			if old2 then
                old2:Destroy()
            end
            cell.btn.Visible=false
			cell.gou.Visible=false
			cell.icon.Visible=false
			cell.amount.Visible=false
			cell.time.Visible=false
            cell.Viewport.Visible=false
			cell.CF.Value=CFrame.new(0, 0, -2);
            if v.icon then
                cell.icon.Image=v.icon
                cell.icon.Visible=true
                cell.amount.Visible=true
                cell.amount.Text="x"..BigNumberHelper:ToString(v.count) 
            end
            if v.model then
				if(v.model:IsA("Model"))then
					v.model.Parent=cell.Viewport
					v.model:SetPrimaryPartCFrame(cell.CF.Value)
					else
						cell.CF.Value=CFrame.new(0, 0, -8);
						v.model.CFrame=cell.CF.Value;
						v.model.Parent=workspace;
						task.wait();
						v.model.Parent=cell.Viewport;
				end
				
                cell.Viewport.Visible=true
                -- cell.Name.Text=v.Cfg.name
                -- cell.Name.TextColor3=v.Cfg.nameColor
                -- cell.Name.Visible=true
            end
			cell.time.Visible=true;
			cell.btn.Visible=true;
        end,

        listener=function(this)
            this.data.DataChange:Connect(function()
                if  not this.data:IsClaimed() then
                    this:FullUi()
                end
            end)
        end
    }
    Obj:FullUi()
    Obj:listener()
end

function M.Update(self,dt)
    local min=math.huge
    local count=0
	local totalCount=0;--已经领取和可领取的数量
    for k,v in pairs(self.OnlineData) do
        if v:IsCanGet() then
            self.insts.content[k].time.Text=LanguageHelper:GetText(10403)
			self.insts.content[k].btn.ImageColor3=self.insts.content[k].colorNow.Value;
            count=count+1
			totalCount=totalCount+1;
        elseif v:IsClaimed() then
            self.insts.content[k].time.Text=LanguageHelper:GetText(10404)
			self.insts.content[k].btn.ImageColor3=self.insts.content[k].colorOld.Value;
			totalCount=totalCount+1;
        else
            if  v.time>0 then
                v.time=v.time-1
                self.insts.content[k].time.Text=GeneralTool.secondsToHHMM(v.time)
				self.insts.content[k].btn.ImageColor3=self.insts.content[k].colorWill.Value;           
            end
            if min>v.time then
                min=v.time
            end
        end
    end
    self.MinTimeAction:Action(count,GeneralTool.secondsToHHMM(min))
	if totalCount== #self.OnlineData then
		-- self.updateConnect:Disconnect();
		-- self.updateConnect=nil;
		-- print(self.updateConnect,"结束")
		if self.update_connect then
			self.update_connect:Disconnect() 
			self.update_connect=nil
		end
		self.ClaimFreeAction:Action();
	end
end

function M.StartUpdateTime(self)
    if self.update_connect then
        return 
    end

    self.update_connect=self:AddStepUpdate(self.Update,1)
end
function M.SetRewardCell(self,count)
	for i = 1, count, 1 do
		if( not self.insts.content[i]) then
			local cell= self.insts.cell:Clone();
			cell.Parent=self.insts.cells;
			cell.Name=i;
			cell.Visible=true;	
			local data={
				Viewport="Viewport",
				CF="Viewport.CF",
				btn="btn",
				colorNow="btn.nowC",
				colorOld="btn.oldC",
				colorWill="btn.willC",
				gou="gou",
				icon="icon",
				amount="Amount",
				time="Time",
			}
			self.insts.content[i]=self:InitUiInst(cell,data)
		end
		
	end
end;
function M.dtor(self)
	M.super.dtor(self);
end;
return M;